import React, { useEffect, useState } from 'react'
import { Table, message, Button } from 'antd';
import { fetchRequest } from '../../../../../utils/Request';
import { url } from '../../../../../utils/url';
import { useRequest } from '@umijs/hooks';

export default function DeviceInfoList(props: any) {
  console.log(props)
  const taskIds = props.content.filter((i: any) => i.flag === 1).map((i: any) => i.taskId)


  const { data: resendData, run } = useRequest((params) => fetchRequest(url.ACCESS_CARD_RESEND, params), {
    manual: true,
    onSuccess: (data) => {
      if (data.success) {
        message.success(data.data)


        props.onClose()


      } else {
        message.error(data.errorMsg)
      }

    }
  })

  const columns = [
    {
      title: "设备名称",
      dataIndex: "deviceName",
      key: "deviceName",
      align: "center" as "center"
    },
    {
      title: "设备状态",
      dataIndex: "deviceState",
      key: "deviceState",
      align: "center" as "center"
    },
    {
      title: "开门方式",
      dataIndex: "openType",
      key: "openType",
      align: "center" as "center"
    }, {
      title: <>
        <span >操作</span>
        {taskIds.length !== 0 && <Button type="primary" style={{ marginLeft: '20px' }} size="small" onClick={() => againClick()}>一键推送</Button>}

      </>,
      dataIndex: "",
      render: (data: any) => {

        return <>
          {data.flag === 1 && <Button onClick={() => againClick(data)} type="primary">重新推送</Button>}

        </>
      }
    }
  ];
  /*重新推送*/
  const againClick = async (data?: any) => {
    let taskIdList = []
    if (data) {


      taskIdList = [data.taskId]
    } else {
      taskIdList = taskIds
    }

    await run({ taskIds: taskIdList })
  }


  const [data, setdata] = useState([])

  async function getData() {
    try {
      const json = await fetchRequest(url.GET_DEVICE_LIST_INFO, {
        accessCardId: props.cardID,
        deptId: props.deptID.toString()
      });
      if (json.success) {
        setdata(json.data)
      } else {
        message.destroy();
        message.error(json.error)
      }
    } catch (error: any) {
      message.destroy();
      message.error(error.message)
    }
  }

  // const getDeviceData = useRequest(getData, {
  //     loadingDelay: 200
  // })

  return (
    <Table columns={columns} dataSource={props.content} />
  )
}
